<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.DateFormat"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
  out.println(application.getInitParameter("name"));
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新闻中国</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
	function check(){
		var login_username = document.getElementById("uname");
		var login_password = document.getElementById("upwd");
		if(login_username.value == ""){
			alert("用户名不能为空！请重新填入！");
			login_username.focus();	
			return false;
		}else if(login_password.value == ""){
			alert("密码不能为空！请重新填入！");
			login_password.focus();
			return false;
		}
		return true;
	}
	
	function focusOnLogin(){
		var login_username = document.getElementById("uname");
		login_username.focus();	
	}

	$(function () {
        getNewsByPage(0);
    });
  function getNewsByPage(ntid){
	    if(ntid == undefined||ntid == null)ntid=0;
      $.getJSON("/news.do",{"opr":"getNewsByPage","ntid":ntid,"currPageNo":1,"pageSize":15},function (result) {
          //加载一次清空一次
          $(".classlist").empty();
          if(result == null){
              $(".classlist").html("请求失败！！！");
          }else if(result.totalCount == 0){
              $(".classlist").html("抱歉，没有找到相关的新闻")
          }else{//查询到内容
              //新闻集合
              $(result.data).each(function (i,news) {
                  $(".classlist").append(
                      "<li>" +
                      "<a href='/user/news_read.jsp?nid="+news.nid+"'>"+news.ntid+"</a>"+
                      "<span>"+news.ncreateDate+"</span>"+
                      "</li>"
                  );
                  if((i+1)%5 == 0){
                      $(".classlist").append("<li class='space'></li>");
                  }
              })
              //分页标签
              $("#currPageNo").html(result.currPageNo);
              $("#totalCount").html(result.totalCount);
              $("#totalPageCount").html(result.totalPageCount);
          }
      })
  }
</script>
</head>

<body onload="">
  
<div id="header">
  <div id="top_login">
    <c:if test="${empty sessionScope.login}">
      <form action="/user.do" method="post" onsubmit="return check()">
        <input type="hidden" name="opr" value="login"/>
        <label> 登录名 </label>
        <input type="text" id="uname" name="uname" value="" class="login_input" />
        <label> 密&#160;&#160;码 </label>
        <input type="password" id="upwd"  name="upwd" value="" class="login_input" />
        <input type="submit" class="login_sub" value="登录" />
        <label id="error">
          <c:if test="${not empty sessionScope.login_error}">
            <span style="color:red;">${login_error}</span>
            <c:remove var="login_error" scope="session"></c:remove>
          </c:if>
        </label>
        文件存放地址：${initParam.fileupload}
        <img src="images/friend_logo.gif" alt="Google" id="friend_logo" />
      </form>
    </c:if>
    <c:if test="${not empty sessionScope.login}">
      <span>欢迎：${sessionScope.login.user.uname}</span>
      <a href="/user.do?opr=loginOut">注销</a>
    </c:if>
  </div>
  <div id="nav">
    <div id="logo"> <img src="images/logo.jpg" alt="新闻中国" /> </div>
    <div id="a_b01"> <img src="images/a_b01.gif" alt="" /> </div>
    <!--mainnav end-->
  </div>
</div>
<div id="container">

<%@ include file="index-elements/index_sidebar.jsp"%>
 
  <div class="main">
    <div class="class_type"> <img src="images/class_type.gif" alt="新闻中心" /> </div>
    <div class="content">
      <ul class="class_date">
        <a href="javascript:getNewsByPage">全部</a>
      <c:forEach items="${topicList}" var="topic" varStatus="i">
        <c:if test="${i.count % 11 == 1}"><li id='class_month'></c:if>
        <a href="util/news?opr=listTitle&tid=${topic.tid}"><b>${topic.tname}</b></a>
        <c:if test="${i.count % 11 == 0}"></li></c:if>
        <c:set var="n" value="${i.count}"/>
      </c:forEach>
      <c:if test="${n % 11 != 0}"></li></c:if>
      </ul>
      <ul class="classlist">
      <c:choose>
        <c:when test="${requestScope.list4 == null}"><h6>出现错误，请稍后再试或与管理员联系</h6></c:when>
        <c:when test="${empty requestScope.list4}"><h6>抱歉，没有找到相关的新闻</h6></c:when>
        <c:otherwise>
          <c:forEach items="${requestScope.list4}" var="news" varStatus="i">
            <li>
            <a href='util/news?opr=readNew&nid=${news.nid}'>${news.ntitle}</a>
            <span><fmt:formatDate value="${news.ncreatedate}" pattern="yyyy-MM-dd HH:mm:ss"/></span>
            </li>
            <c:if test="${i.count % 5 == 0}"><li class='space'></li></c:if>
          </c:forEach>
        </c:otherwise>
      </c:choose>
        <p align="center"> 当前页数:[<span id="currPageNo"></span>/<span id="totalPageCount"></span>]&nbsp;
          总条数：<span id="totalCount"></span>
      <c:if test="${pageIndex > 1}">   
          <a href="util/news?opr=listTitle&pageIndex=1">首页</a>&nbsp;
          <a href="util/news?opr=listTitle&pageIndex=${pageIndex - 1}">上一页</a>
      </c:if>
      <c:if test="${pageIndex < totalPages}">
          <a href="util/news?opr=listTitle&pageIndex=${pageIndex + 1}">下一页</a>
          <a href="util/news?opr=listTitle&pageIndex=${totalPages}">末页</a> 
      </c:if></p>
      </ul>
    </div>
    <%@ include file="index-elements/index_rightbar1.jsp"%>
  </div>
</div>
  <%@ include file="index-elements/index_bottom.html"%>
</body>
</html>